package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppNewsLogMapper;
import com.eco.fanliapp.dao.AppUserMapper;
import com.eco.fanliapp.dao.MessagePushMapper;
import com.eco.fanliapp.entity.AppNewsLog;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.MessagePush;
import com.eco.fanliapp.service.IAppNewsLogService;
import com.eco.fanliapp.service.IMessagePushService;
import com.eco.fanliapp.service.JpushMessage;
import com.eco.fanliapp.utils.EmojiUtil;
import com.eco.fanliapp.utils.StringUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessagePushServiceImpl extends ServiceImpl<MessagePushMapper, MessagePush> implements IMessagePushService {

    @Autowired
    private MessagePushMapper messagePushMapper;

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private AppNewsLogMapper newsLogMapper;
    @Autowired
    private IAppNewsLogService appNewsLogService;

    @Override
    public ServerResponse insert(MessagePush messagePush, Integer timeType) {
        Integer insert = 1;
        if (timeType != 3) {
            messagePush.onUpdate();
            if (timeType == 1) {
                messagePush.setStatus(1);
                messagePush.onCreate();
                insert = messagePushMapper.insert(messagePush);
            } else if (timeType == 2) {
                messagePush.setStatus(0);
                messagePush.onCreate();
                messagePushMapper.insert(messagePush);
                return ServerResponse.createBySuccessMessage("操作成功");
            }
        }
        List<AppNewsLog> newsLogList = new ArrayList<>();
        List<String> userIdlist = new ArrayList<>();
        if (insert > 0) {

            if (messagePush.getType() == 3) {
                //发送全部用户 10001
                AppUser newAppUser = new AppUser();
                List<AppUser> list = null;
                if (messagePush.getCompanyId() == 0) {
                    newAppUser.setAppLogin(1);
                    list = appUserMapper.selectAppUserList(newAppUser);
                } else {
                    newAppUser.setAppLogin(1);
                    newAppUser.setCompanyId(messagePush.getCompanyId());
                    list = appUserMapper.selectAppUserList(newAppUser);
                }
                for (AppUser appUser : list) {
                    AppNewsLog appNewsLog = getAppNewsLog(messagePush);
                    appNewsLog.setUserId(appUser.getUserId());
                    appNewsLog.setNewsType(10001);
                    appNewsLog.setJumpLink(messagePush.getJumpLink());
                    appNewsLog.setJumpType(messagePush.getJumpType());
                    appNewsLog.setLinkType(messagePush.getJumpTaobao());
                    newsLogList.add(appNewsLog);
                    userIdlist.add(String.valueOf(appUser.getUserId()));
                }
            } else if (messagePush.getType() == 1) {
                //下单用户 10002
                List<AppUser> list = appUserMapper.findUserIn(messagePush.getCompanyId());
                for (AppUser appUser : list) {
                    AppNewsLog appNewsLog = getAppNewsLog(messagePush);
                    appNewsLog.setUserId(appUser.getUserId());
                    appNewsLog.setJumpLink(messagePush.getJumpLink());
                    appNewsLog.setJumpType(messagePush.getJumpType());
                    appNewsLog.setLinkType(messagePush.getJumpTaobao());
                    appNewsLog.setNewsType(10002);
                    newsLogList.add(appNewsLog);
                    userIdlist.add(String.valueOf(appUser.getUserId()));
                }
            } else if (messagePush.getType() == 2) {//未下单用户 10003
                List<AppUser> list = appUserMapper.findUserNotIn(messagePush.getCompanyId());
                for (AppUser appUser : list) {
                    AppNewsLog appNewsLog = getAppNewsLog(messagePush);
                    appNewsLog.setUserId(appUser.getUserId());
                    appNewsLog.setJumpLink(messagePush.getJumpLink());
                    appNewsLog.setJumpType(messagePush.getJumpType());
                    appNewsLog.setLinkType(messagePush.getJumpTaobao());
                    appNewsLog.setNewsType(10003);
                    newsLogList.add(appNewsLog);
                    userIdlist.add(String.valueOf(appUser.getUserId()));
                }
            }
            if (CollectionUtils.isNotEmpty(newsLogList)) {
                List<List<AppNewsLog>> partition = Lists.partition(newsLogList, 1000);
                List<List<String>> subUserIdList = Lists.partition(userIdlist, 1000);
                int number = 0;
                for (List<AppNewsLog> appNewsLogs : partition) {
                    userIdlist = subUserIdList.get(number);
                    Boolean i = appNewsLogService.saveBatch(appNewsLogs);
                    if (i) {
                        userIdlist = subUserIdList.get(number);

                        JpushMessage.jiGuangPushMesageListAndroid(10001, EmojiUtil.resolveToEmojiFromByte(messagePush.getTitle()), userIdlist, EmojiUtil.resolveToEmojiFromByte(messagePush.getContent()), messagePush.getJumpType(), messagePush.getJumpLink(),messagePush.getJumpTaobao());
                        JpushMessage.jiGuangPushMesageListIos(10001, EmojiUtil.resolveToEmojiFromByte(messagePush.getTitle()), userIdlist, EmojiUtil.resolveToEmojiFromByte(messagePush.getContent()), messagePush.getJumpType(), messagePush.getJumpLink(),messagePush.getJumpTaobao());

                        number += 1;
                    }

                }
            }
        }
        return ServerResponse.createBySuccess(insert);
    }

    private AppNewsLog getAppNewsLog(MessagePush messagePush) {
        AppNewsLog appNewsLog = new AppNewsLog();
        //活动头像
        appNewsLog.setIconUrl(ApiEnum.MSG_ACTION_ICON);
        appNewsLog.setNewsTitle(EmojiUtil.resolveToEmojiFromByte(messagePush.getTitle()));
        appNewsLog.setNewsContent(EmojiUtil.resolveToEmojiFromByte(messagePush.getContent()));
        appNewsLog.setCreateTime(System.currentTimeMillis());
        appNewsLog.setUpdateTime(System.currentTimeMillis());
        return appNewsLog;
    }
}
